﻿
@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="SAMPLE-AddMethod-MethodId">@ViewData["MethodId"]</label>
            <label id="SAMPLE-AddMethod-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">代码*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddMethod-Code" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddMethod-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">类型*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddMethod-Select-Type" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddMethod-Input-Type" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">版本*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddMethod-Version" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">生效日期</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SampleMethod-EffectiveDate" type="text" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">有效期至</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SampleMethod-ExpireDate" type="text" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">记录单</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddMethod-RecordSheet" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddMethod-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <br />
    <div class="text-right">
        <button id="SAMPLE-AddMethod-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="SAMPLE-AddMethod-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>
    var AddMethod_Id = $("#SAMPLE-AddMethod-MethodId").html();

    /*初始化页面控件值，加载方法类型*/
    $(document).ready(function() { 
        LoadSampleMethodType();
        LoadRecordSheet();

        $('#SampleMethod-EffectiveDate').datetimepicker({
            timepicker: false,
            format: "Y/m/d"
        });
        $('#SampleMethod-ExpireDate').datetimepicker({
            timepicker: false,
            format: "Y/m/d"
        });
        $.datetimepicker.setLocale('zh');  // 设置时间控件为中文

        if (AddMethod_Id.length != 0) {
            // 如果是编辑状态，初始化页面控件值
            $.post("GetMethod", {
                Id: AddMethod_Id
            }, function (data) {
                $("#SAMPLE-AddMethod-Code").val(data.code);
                $("#SAMPLE-AddMethod-Name").val(data.name);
                $("#SAMPLE-AddMethod-Input-Type").val(data.type);
                $("#SAMPLE-AddMethod-Version").val(data.version);
                $('#SampleMethod-EffectiveDate').val(data.effectiveDate);
                $('#SampleMethod-ExpireDate').val(data.expireDate);
                $("#SAMPLE-AddMethod-Description").val(data.description);
                $("#SAMPLE-AddMethod-RecordSheet").val(data.recordSheet);
            });
        }
    });

    /*加载方法类型*/
    function LoadSampleMethodType() {
        $.post("GetMethodType", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddMethod-Select-Type").append(new Option(data[i].type));
            }
            $("#SAMPLE-AddMethod-Select-Type").prepend(new Option("", ""));
            $("#SAMPLE-AddMethod-Select-Type").val("");
        });
    }

    function LoadRecordSheet() {
        $.post("GetRecordSheet", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            $("#SAMPLE-AddMethod-RecordSheet").prepend(new Option("", ""));
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddMethod-RecordSheet").append(new Option(data[i]));
            }
        });
    }

    $("#SAMPLE-AddMethod-Select-Type").change(function () {
        $("#SAMPLE-AddMethod-Input-Type").val($("#SAMPLE-AddMethod-Select-Type").find("option:selected").text());
        $("#SAMPLE-AddSampleDefinition-Input-GenricClass").val('');
    });

    $("#SAMPLE-AddMethod-confirm").click(function () {
        if ($.trim($("#SAMPLE-AddMethod-Code").val()).length == 0) {
            alert("方法代码不能为空！");
            $("#SAMPLE-AddMethod-Code").addClass('error-style');
            return false;
        }

        if ($.trim($("#SAMPLE-AddMethod-Name").val()).length == 0) {
            alert("方法名称不能为空！");
            $("#SAMPLE-AddMethod-Name").addClass('error-style');
            return false;
        }

        if ($.trim($("#SAMPLE-AddMethod-Input-Type").val()).length == 0) {
            alert("方法类型不能为空！");
            $("#SAMPLE-AddMethod-Input-Type").addClass('error-style');
            return false;
        }

        if ($.trim($("#SAMPLE-AddMethod-Version").val()).length == 0) {
            alert("方法版本不能为空！");
            $("#SAMPLE-AddMethod-Version").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Code"] = $.trim($("#SAMPLE-AddMethod-Code").val());
        jsonStr["Name"] = $.trim($("#SAMPLE-AddMethod-Name").val());
        jsonStr["Type"] = $.trim($("#SAMPLE-AddMethod-Input-Type").val());
        jsonStr["Version"] = $.trim($("#SAMPLE-AddMethod-Version").val());
        jsonStr["EffectiveDate"] = $('#SampleMethod-EffectiveDate').val();
        jsonStr["ExpireDate"] = $('#SampleMethod-ExpireDate').val();
        jsonStr["Description"] = $.trim($("#SAMPLE-AddMethod-Description").val());
        jsonStr["RecordSheet"] = $("#SAMPLE-AddMethod-RecordSheet").find("option:selected").text();

        if (AddMethod_Id.length == 0) {
            $.post("postAddMethod", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                alert(data);
                var modalID = $("#SAMPLE-AddMethod-Parent-ModalID").html(); // 这两行代码不能提到最后面，否则因为异步会造成刷新不到数据。
                $("#" + modalID).modal('hide');
            });
        } else {
            jsonStr["Id"] = AddMethod_Id;
            $.post("postUpdateMethod", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                alert(data);
                var modalID = $("#SAMPLE-AddMethod-Parent-ModalID").html();
                $("#" + modalID).modal('hide');
            });
        }
    });

    $("#SAMPLE-AddMethod-cancel").click(function () {
        var modalID = $("#SAMPLE-AddMethod-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });
    
</script>
